package com.gitee.neuray.security.generator.strategy;

import java.util.List;

/**
 * @author: zhhongyu
 * @description: sqlserver策略
 * @since: Create in 14:38 2020/2/14
 */
public class SQLServerStrategy extends DataSourceStrategy {
    @Override
    public StringBuilder queryColumnsSQL(String viewOrtable) {
        //todo:mysql策略少第六个参数，dataLength（长度），sqlserver也是，后期支持mysql时注意改造
        StringBuilder sql = new StringBuilder(32);
        sql.append(" SELECT "  );
        sql.append(      "CAST (b. NAME AS VARCHAR(500)) AS columnName, "  );
        sql.append(      "CAST ( "  );
        sql.append(      " sys. TYPES . NAME AS VARCHAR (500) "  );
        sql.append(       " ) AS dataType, "  );
        sql.append(       "CAST (c. VALUE AS VARCHAR(500)) AS columnComment, "  );
        sql.append(    " ( "  );
        sql.append(       " SELECT "  );
        sql.append(     "  CASE COUNT (1) "  );
        sql.append(     " WHEN 1 THEN "  );
        sql.append(      "   'PRI' "  );
        sql.append(      " ELSE "  );
        sql.append(     "   '' "  );
        sql.append(      " END "  );
        sql.append(     " FROM "  );
        sql.append(    "  syscolumns, "  );
        sql.append(     "  sysobjects, "  );
        sql.append(    "  sysindexes, "  );
        sql.append(    "  sysindexkeys, "  );
        sql.append(     "  systypes "  );
        sql.append(     " WHERE "  );
        sql.append(     "   syscolumns.xusertype = systypes.xusertype "  );
        sql.append(     "  AND syscolumns. ID = object_id (A . NAME) "  );
        sql.append(      "  AND sysobjects.xtype = 'PK' "  );
        sql.append(      "  AND sysobjects.parent_obj = syscolumns. ID "  );
        sql.append(     "  AND sysindexes. ID = syscolumns. ID "  );
        sql.append(     "  AND sysobjects. NAME = sysindexes. NAME "  );
        sql.append(    "  AND sysindexkeys. ID = syscolumns. ID "  );
        sql.append(     "  AND sysindexkeys.indid = sysindexes.indid "  );
        sql.append(     "  AND syscolumns.colid = sysindexkeys.colid "  );
        sql.append(     "  AND syscolumns. NAME = B. NAME "  );
        sql.append(    " ) AS columnKey, "  );
        sql.append(     " '' AS EXTRA "  );
        sql.append(     "FROM "  );
        sql.append(    " ( "  );
        sql.append(     "  SELECT "  );
        sql.append(    "   NAME, "  );
        sql.append(   "   object_id "  );
        sql.append(   "  FROM "  );
        sql.append(   "   sys. TABLES "  );
        sql.append(     "  UNION ALL "  );
        sql.append(     "   SELECT "  );
        sql.append(    "    NAME, "  );
        sql.append(     "    object_id "  );
        sql.append(    "   FROM "  );
        sql.append(    "    sys. VIEWS "  );
        sql.append(   " ) A "  );
        sql.append(   "INNER JOIN sys. COLUMNS b ON b.object_id = A .object_id "  );
        sql.append(   "LEFT JOIN sys. TYPES ON b.user_type_id = sys. TYPES .user_type_id "  );
        sql.append(   "LEFT JOIN sys.extended_properties c ON c.major_id = b.object_id "  );
        sql.append(   "AND c.minor_id = b.column_id "  );
        sql.append(   "WHERE "  );
        sql.append(   " A . NAME =  "  );
        sql.append(viewOrtable);
        sql.append(   "AND sys. TYPES . NAME != 'sysname'");
        return sql;
    }

    @Override
    public StringBuilder queryTablesSQL() {
        //todo:sqlserver
        return null;
    }

    @Override
    public StringBuilder queryTableSQL(String tableName) {
        return null;
    }
}
